package com.zelin.yang.kotlin_demo.page

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import com.shehuan.wanandroid.base.net.RequestManager
import com.shehuan.wanandroid.base.net.RetrofitManager
import com.shehuan.wanandroid.base.net.exception.ResponseException
import com.shehuan.wanandroid.base.net.observer.BaseObserver
import com.zelin.yang.kotlin_demo.Base.BaseActivity
import com.zelin.yang.kotlin_demo.R
import com.zelin.yang.kotlin_demo.entry.LoginBean
import com.zelin.yang.kotlin_demo.net.WanAndroidApis
import com.zelin.yang.kotlin_demo.util.SpUtil
import com.zelin.yang.kotlin_demo.util.ToastUtil
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : BaseActivity() {

    companion object {
        fun start(context: Context) {
            val intent = Intent(context, LoginActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }

    override fun Initview() {
        loginBtn.setOnClickListener {
            if(TextUtils.isEmpty(loginUsernameET.text) || TextUtils.isEmpty(loginPasswordET.text)){
                ToastUtil.showToast(mContext,"账号或密码不可以为空")
            }else{
                login(loginUsernameET.text.toString(),loginPasswordET.text.toString())
            }
        }

        registerTv.setOnClickListener {
            ToastUtil.showToast(mContext,"跳到注册页")
        }

    }

    override fun initDate() {

    }

    private fun login(name:String,pass:String) {
        val map = hashMapOf<String, String>()
        map.put("username", name)
        map.put("password", pass)
        RequestManager.execute(RetrofitManager.create(WanAndroidApis::class.java).login(map), object : BaseObserver<LoginBean>() {
            override fun onSuccess(data: LoginBean) {
                SpUtil.setUsername(name)
                finish()
                ToastUtil.showToast(mContext, "登陆成功")
            }

            override fun onError(e: ResponseException) {

            }
        })
    }

}